package com.lawyer.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.persistence.EntityManager;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lawyer.dynamic.Criteria;
import com.lawyer.dynamic.Restrictions;
import com.lawyer.entity.User;
import com.lawyer.model.Message;
import com.lawyer.model.MessageConstant;
import com.lawyer.repository.MoneyFlowsRepository;
import com.lawyer.repository.SystemUserRepository;
import com.lawyer.repository.UserRepository;

@RestController
@RequestMapping(value = "/system")
@Api(tags = "系统管理-代理")
@SuppressWarnings("all")
public class SystemAgentController {

	protected final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private SystemUserRepository systemUserRepository;

	@Autowired
	private UserRepository userRepository;

	@Autowired
	private MoneyFlowsRepository moneyFlowsRepository;
	@Autowired
	private EntityManager entityManager;
	

	@ApiOperation(httpMethod = "GET", value = "查询所有代理", notes = "查询所有代理")
	@RequestMapping(value = "/agents", method = RequestMethod.GET)
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "page", dataType = "integer", paramType = "query", value = "第几页"),
		@ApiImplicitParam(name = "size", dataType = "integer", paramType = "query", value = "分页条数") })
	public HttpEntity<?> agents(String nickName, String phone,@PageableDefault(page = 0, direction = Sort.Direction.DESC) Pageable pageable) {

		Message msg = new Message();
		try {
			Criteria<User> criteria = new Criteria<User>();

			if (StringUtils.isNotEmpty(nickName)) {
				criteria.add(Restrictions.like("nickName", "%" + nickName + "%", true));
			}

			if (StringUtils.isNotEmpty(phone)) {
				criteria.add(Restrictions.like("phone", phone, true));
			}
			
			criteria.add(Restrictions.eq("userType", 1, true));

			Page<User> page = userRepository.findAll(criteria,pageable);
			msg.setData(page);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResponseEntity<>(msg, HttpStatus.OK);
	
		
	}

	@ApiOperation(httpMethod = "GET", value = "查询代理所有绑定用户", notes = "查询代理所绑定的用户")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "page", dataType = "integer", paramType = "query", value = "第几页"),
		@ApiImplicitParam(name = "size", dataType = "integer", paramType = "query", value = "分页条数") })

	@RequestMapping(value = "/childs", method = RequestMethod.GET)
	public HttpEntity<?> childs(
			@RequestParam(required = true)String agentId,
			@PageableDefault(page = 0, direction = Sort.Direction.DESC) Pageable pageable
			) {
		Message msg = new Message();
		try {
			Page<User> list = userRepository.findByAgentId(agentId,pageable);
			for (User user : list) {
				user.setPassword(null);
			}
			msg.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
			msg.setCode(MessageConstant.FAILED_CODE);
			msg.setMessage(MessageConstant.FAILED_MSG);
		}
		return new ResponseEntity<>(msg, HttpStatus.OK);
	}



	@ApiOperation(httpMethod = "PUT", value = "开通代理", notes = "开通代理")
	@RequestMapping(value = "/ascension", method = RequestMethod.PUT)
	public HttpEntity<?> ascension(String userId) {
		Message msg = new Message();
		try {
			User user = userRepository.findOne(userId);
			if (user != null) {
				user.setUserType("1");
				userRepository.save(user);
			} else {
				msg.setCode(MessageConstant.FAILED_CODE);
				msg.setMessage("账号不存在");
			}

		} catch (Exception e) {
			e.printStackTrace();
			msg.setCode(MessageConstant.FAILED_CODE);
			msg.setMessage(MessageConstant.FAILED_MSG);
		}

		return new ResponseEntity<>(msg, HttpStatus.OK);
	}
	
	@ApiOperation(httpMethod = "PUT", value = "取消代理", notes = "取消代理")
	@RequestMapping(value = "/cancelAgent", method = RequestMethod.PUT)
	public HttpEntity<?> cancelAgent(String userId) {
		Message msg = new Message();
		try {
			User user = userRepository.findOne(userId);
			if (user != null) {
				user.setUserType("0");
				userRepository.save(user);
			} else {
				msg.setCode(MessageConstant.FAILED_CODE);
				msg.setMessage("账号不存在");
			}

		} catch (Exception e) {
			e.printStackTrace();
			msg.setCode(MessageConstant.FAILED_CODE);
			msg.setMessage(MessageConstant.FAILED_MSG);
		}

		return new ResponseEntity<>(msg, HttpStatus.OK);
	}
}
